package QueueAndStack;

import java.util.*;

public class QueueToStack {

    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public QueueToStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }


    public void push(int x) {
        if (empty()) {
            queue1.offer(x);
            return;
        }

        if ( !queue1.isEmpty() && queue2.isEmpty()) {
            queue1.offer(x);
        }else if ( !queue2.isEmpty() && queue1.isEmpty()) {
            queue2.offer(x);
        }
    }

    public int pop() {
        if ( !queue1.isEmpty() && queue2.isEmpty()){
            int temp = queue1.size()-1;
            for (int i = 0; i < temp; i++) {
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        } else if ( !queue2.isEmpty() && queue1.isEmpty()) {
            int temp = queue2.size() - 1;
            for (int i = 0; i < temp; i++) {
                queue1.offer(queue2.poll());

            }
            return queue2.poll();
        }
        return -1;

    }

    public int top() {
        if ( !queue1.isEmpty() && queue2.isEmpty()){
            int temp = queue1.size()-1;
            for (int i = 0; i < temp; i++) {
                queue2.offer(queue1.poll());
            }
            int temp1 = queue1.poll();
            queue2.offer(temp1);
            return temp1;
        } else if ( !queue2.isEmpty() && queue1.isEmpty()) {
            int temp = queue2.size() - 1;
            for (int i = 0; i < temp; i++) {
                queue1.offer(queue2.poll());

            }
            int temp1 = queue2.poll();
            queue1.offer(temp1);
            return temp1;
        }
        return -1;

    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }

}
